{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tkinter as tk\n",
    "from tkinter import filedialog\n",
    "import pyautogui\n",
    "from PIL import Image\n",
    "import win32con\n",
    "import win32gui\n",
    "import win32api\n",
    "import threading"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 全局变量\n",
    "start_x, start_y = None, None\n",
    "end_x, end_y = None, None\n",
    "screenshot = None\n",
    "root = tk.Tk()\n",
    "root.withdraw()\n",
    "\n",
    "# 定义鼠标拖动截屏相关函数\n",
    "def on_mouse_down(event):\n",
    "    global start_x, start_y\n",
    "    start_x = event.x_root\n",
    "    start_y = event.y_root\n",
    "\n",
    "\n",
    "def on_mouse_up(event):\n",
    "    global end_x, end_y, screenshot\n",
    "    end_x = event.x_root\n",
    "    end_y = event.y_root\n",
    "    # 截取指定区域的屏幕\n",
    "    screenshot = pyautogui.screenshot(region=(min(start_x, end_x), min(start_y, end_y),\n",
    "                                              abs(end_x - start_x), abs(end_y - start_y)))\n",
    "    # 关闭截屏窗口\n",
    "    capture_window.destroy()\n",
    "    # 弹出保存对话框\n",
    "    save_screenshot()\n",
    "\n",
    "\n",
    "def save_screenshot():\n",
    "    if screenshot:\n",
    "        file_path = filedialog.asksaveasfilename(defaultextension=\".png\",\n",
    "                                                 filetypes=[(\"PNG files\", \"*.png\"), (\"JPEG files\", \"*.jpg\")])\n",
    "        if file_path:\n",
    "            screenshot.save(file_path)\n",
    "            print(f\"截图已保存到 {file_path}\")\n",
    "\n",
    "\n",
    "# 定义全局快捷键回调函数\n",
    "def capture_screenshot():\n",
    "    global capture_window\n",
    "    # 创建全屏透明窗口用于截屏\n",
    "    capture_window = tk.Toplevel()\n",
    "    capture_window.attributes('-fullscreen', True)\n",
    "    capture_window.attributes('-alpha', 0.1)\n",
    "    capture_window.bind(\"<ButtonPress-1>\", on_mouse_down)\n",
    "    capture_window.bind(\"<ButtonRelease-1>\", on_mouse_up)\n",
    "    capture_window.mainloop()\n",
    "\n",
    "\n",
    "# 全局快捷键监听线程函数\n",
    "def hotkey_listener():\n",
    "    # 注册 Ctrl + Shift + X 快捷键\n",
    "    MOD_CONTROL = 0x0002\n",
    "    MOD_SHIFT = 0x0004\n",
    "    VK_X = 0x58\n",
    "    hotkey_id = 1\n",
    "    win32gui.RegisterHotKey(None, hotkey_id, MOD_CONTROL | MOD_SHIFT, VK_X)\n",
    "    try:\n",
    "        msg = win32gui.GetMessage(None, 0, 0)\n",
    "        while msg[0] != win32con.WM_QUIT:\n",
    "            if msg[0] == win32con.WM_HOTKEY:\n",
    "                if msg[3] == hotkey_id:\n",
    "                    capture_screenshot()\n",
    "            msg = win32gui.GetMessage(None, 0, 0)\n",
    "    finally:\n",
    "        win32api.UnregisterHotKey(None, hotkey_id)\n",
    "\n",
    "\n",
    "# 启动全局快捷键监听线程\n",
    "hotkey_thread = threading.Thread(target=hotkey_listener)\n",
    "hotkey_thread.start()\n",
    "\n",
    "# 运行主循环\n",
    "root.mainloop()\n",
    "    "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
